package main

import "fmt"

//接口方法只有声明，没有实现，由实现类去实现
type Person interface {
	say(n string)
}

type Man interface {
	Person //匿名字段，继承
	play(s string)
}

type Woman interface {
	Person
	sing(s string)
}

type Boy struct {
	name string
}

func (this *Boy) say(n string) {
	fmt.Println("boy: ", this.name, " say:", n)
}

func (this *Boy) play(n string) {
	fmt.Println("boy: ", this.name, " play:", n)
}

type Girl struct {
	name string
}

func (this *Girl) say(n string) {
	fmt.Println("girl: ", this.name, " say:", n)
}

func (this *Girl) sing(n string) {
	fmt.Println("girl: ", this.name, " sing:", n)
}

func WhoSay(p Person, s string) {
	p.say(s)
}

//接口的定义和实现
func main() {
	var p Person
	p = &Boy{"tom"}
	p.say("hello")
	p = &Girl{"lily"}
	p.say("ok")
	//接口多态表现
	WhoSay(&Boy{"jack"}, "aaaa")
	WhoSay(&Girl{"hanmeimei"}, "bbbb")
	//接口继承
	var m Man
	m = &Boy{"bob"}
	m.say("cccc")
	m.play("football")
	var w Woman
	w = &Girl{"amy"}
	w.say("dddd")
	w.sing("my song")
	//父类可以转换为子类，反之不行
	//空接口，可以接收任意数据类型
	var i interface{} = 1
	fmt.Println("i:", i)
	i = "abc"
	fmt.Println("i:", i)
}
